//
//  WSGQuickNavigationCollectionView.m
//  oneDollar
//
//  Created by Laki on 16/6/16.
//  Copyright © 2016年 com.Laki.corder. All rights reserved.
//

#import "WSGQuickNavigationCollectionView.h"
#import "WSGQuickNavigationCell.h"

#import "WSGQuickNavigationItem.h"

@interface WSGQuickNavigationCollectionView()<UICollectionViewDataSource,UICollectionViewDelegate>
/** UICollectionView*/
@property (nonatomic, weak) UICollectionView *collectionViews;
@end

@implementation WSGQuickNavigationCollectionView
/** 重用标识符*/
NSString * const ID = @"WSGQuickNavigationCell";

/*
 * 在设置Frame里面创建UICollectionView并且布局
 */
- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        // 创建流水布局
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        
        // 创建UICollectionView
        UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        
        // 设置数据源
        collectionView.dataSource = self;
        collectionView.delegate = self;
        // 设置背景颜色
        collectionView.backgroundColor = [UIColor whiteColor];
        // 设置全局
        self.collectionViews = collectionView;
        
        [self addSubview:collectionView];
        
        // 注册UICollectionView
        [collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([WSGQuickNavigationCell class]) bundle:nil] forCellWithReuseIdentifier:ID];
    }
    return self;
}


/*
 * 布局子控件的位置
 */
- (void)layoutSubviews{
    [super layoutSubviews];
    
    // 设置UICollectionView的Frame
    self.collectionViews.frame = self.bounds;
    
    // 获取布局
    UICollectionViewFlowLayout *layout = (UICollectionViewFlowLayout *)self.collectionViews.collectionViewLayout;
    
    // 设置CollectionView的宽高
    CGFloat itemWH = self.collectionViews.bounds.size.height;
    layout.itemSize = CGSizeMake(itemWH, itemWH);
    
    // 计算出列边距,这样四个图标正好能放满一行
    CGFloat spacing = (ScreenW - (self.quickItem.count * itemWH)) / (self.quickItem.count - 1);

    // 列间距
    layout.minimumInteritemSpacing = spacing;
}


#pragma mark --------------------
#pragma mark UICollectionViewDataSource
/*
 * 显示多少个Cell
 */
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.quickItem.count;
}

/*
 * 每行Cell展示什么数据
 */
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    // 从缓存池中取出WSGQuickNavigationCell
    WSGQuickNavigationCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
    
    // 传递模型数据
    cell.navigationItem = self.quickItem[indexPath.item];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    WSGQuickNavigationItem *item = self.quickItem[indexPath.item];
    if ([self.delegate respondsToSelector:@selector(quickNavigationCollection:url:)]) {
        [self.delegate quickNavigationCollection:self url:item.clickUrl];
    }
}
@end
